/**
 * Created by novem on 2015/10/20.
 */
window.onload=function(){
    var supplierId = $.getUrlParam("supplierId");
    var url = getUrl();
    var userId = getUserId();
    $.jsonp({
        url: 'http://'+url+'/api/supplier/supplier_detail-'+supplierId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            var complied_tpl01 = juicer($("#msm_detail01").text());
            var html01 = complied_tpl01.render(data);
            $(".order01").append(html01);
            var shopId = data.supplier.shopId;
            $(".w_shac").attr('data-sc',data.supplier.shopId);
            if(data.state=="已完成"||data.state=="待评价"){
                var html0 ='';
            }else{
                var html0 ='<div class="buy-bar-wrapper">' +
                    '<span class="ui-txt-highlight fz30">'+data.supplier.price+'</span>' +
                    '<span class="ui-txt-highlight">元 </span><s>'+data.supplier.oldPrice+'</s>' +
                    '<a href="jiudian_tijiaodingdan.html?supplierId='+supplierId+'"><button class="ui-btn ui-btn-orange buy-button">立即购买</button></a>' +
                    '</div>'
            }
            $(".buy-bar").append(html0);
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_shops_list',
                data: {userId:userId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        var samewayId = ele.shopsId;
                        if(samewayId==shopId){
                            $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
        },
        error: function (xOptions, textStatus) {
        }
    });
    //收藏
    $(document).on('click','.w_shac',function(){
        var shopsId = $(this).data('sc');
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_shops',
            data: {shopsId:shopsId,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("取消收藏成功")
                }else{
                    $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                    showToast("收藏成功")
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}